import imgUrl from "@/assets/loading.svg"; //引入加载图片
import style from "./loading.module.less";

//创建加载图片
function createImg() {
	const img = document.createElement("img");
	img.src = imgUrl;
	img.className = style.loadingContainer;
	img.dataset.name = "loading";
	return img;
}

//获取加载图片
function getImg(el) {
	// console.log(el.querySelector("img[data-name='loading']"));
	return el.querySelector("img[data-name='loading']");
}

// 该函数会被同时设置到bind和update中
export default function (el, binding) {
	const curImg = getImg(el);
	if (binding.value) {
		if (!curImg) {
			const img = createImg();
			el.appendChild(img);
		}
	} else if (curImg) {
		curImg.remove();
	}
}
